package sos

import (
	"time"
	"xinxiang-mutual-platform/global"
	"xinxiang-mutual-platform/model/common/request"
	"xinxiang-mutual-platform/model/sos"
	"xinxiang-mutual-platform/model/sos/response"
)

type SOSService struct{}

func (sosS *SOSService) CreateSOS(api sos.Sos) (err error) {
	return global.DB.Create(&api).Error
}
func (sosS *SOSService) GetSOSInfoList(req *request.Request) (list interface{}, total int64, err error) {
	db := global.DB
	if req.Condition.InThreeHours == 1 {
		db = db.Where("create_at > ?", time.Now().Add(-time.Hour*76))
	}
	if req.Condition.Extent != "全部" && req.Condition.Extent != "" {
		db = db.Where("extent = ?", req.Condition.Extent)
	}
	if req.Condition.Classify != "全部" && req.Condition.Classify != "" {
		db = db.Where("classify = ?", req.Condition.Classify)
	}
	if req.Condition.Area != "全部" && req.Condition.Area != "" {
		db = db.Where("area = ?", req.Condition.Area).Debug()
	}
	var soslist []sos.Sos
	err = db.Model(&sos.Sos{}).Count(&total).Error
	if err != nil {
		return soslist, 0, db.Error
	}
	err = db.Offset(req.PageInfo.PageSize * (req.PageInfo.Page - 1)).Limit(req.PageInfo.PageSize).Find(&soslist).Debug().Error
	repSoss := make([]response.SosResponse, len(soslist))
	for idx, sos := range soslist {
		strtime := sos.CreateAt.Format("01-02 15:04:05")
		repSoss[idx].Sos = sos
		repSoss[idx].StrTime = strtime
	}
	return repSoss, total, db.Error
}
